<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="本文作者为: SylvanasSun.转载请务必将下面这段话置于文章开头处(保留超链接).本文转发自SylvanasSun Blog,原文链接: https:&#x2F;&#x2F;sylvanassun.github.io&#x2F;2017&#x2F;07&#x2F;27&#x2F;2017-07-27-Graph_WeightedDigraph  加权有向图 有向图的实现比无向图更加简单,要实现加权有向图只需要在上一章讲到的加权无向图的实现修改一下">
<meta property="og:type" content="article">
<meta property="og:title" content="图的那点事儿(4)-加权有向图">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/27/2017-07-27-Graph_WeightedDigraph/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="本文作者为: SylvanasSun.转载请务必将下面这段话置于文章开头处(保留超链接).本文转发自SylvanasSun Blog,原文链接: https:&#x2F;&#x2F;sylvanassun.github.io&#x2F;2017&#x2F;07&#x2F;27&#x2F;2017-07-27-Graph_WeightedDigraph  加权有向图 有向图的实现比无向图更加简单,要实现加权有向图只需要在上一章讲到的加权无向图的实现修改一下">
<meta property="og:locale">
<meta property="og:image" content="http://algs4.cs.princeton.edu/44sp/images/shortest-path.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/44sp/images/spt.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/44sp/images/relaxation-edge.png">
<meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/5/57/Dijkstra_Animation.gif">
<meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/e/e4/DijkstraDemo.gif">
<meta property="article:published_time" content="2017-07-27T04:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="2017">
<meta property="article:tag" content="Algorithms">
<meta property="article:tag" content="Graph">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://algs4.cs.princeton.edu/44sp/images/shortest-path.png">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/27/2017-07-27-Graph_WeightedDigraph/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>图的那点事儿(4)-加权有向图 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A0%E6%9D%83%E6%9C%89%E5%90%91%E5%9B%BE"><span class="nav-number">1.</span> <span class="nav-text">加权有向图</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#DirectedEdge"><span class="nav-number">1.1.</span> <span class="nav-text">DirectedEdge</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#EdgeWeightedDigraph"><span class="nav-number">1.2.</span> <span class="nav-text">EdgeWeightedDigraph</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84"><span class="nav-number">2.</span> <span class="nav-text">最短路径</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8D%95%E7%82%B9%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84"><span class="nav-number">2.1.</span> <span class="nav-text">单点最短路径</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="nav-number">2.2.</span> <span class="nav-text">最短路径的数据结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%A9%E8%BE%B9%E6%9D%BE%E5%BC%9B"><span class="nav-number">2.3.</span> <span class="nav-text">让边松弛</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Dijkstra%E7%AE%97%E6%B3%95"><span class="nav-number">3.</span> <span class="nav-text">Dijkstra算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81"><span class="nav-number">3.1.</span> <span class="nav-text">实现代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A0%E7%8E%AF%E5%8A%A0%E6%9D%83%E6%9C%89%E5%90%91%E5%9B%BE%E4%B8%AD%E7%9A%84%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84%E7%AE%97%E6%B3%95"><span class="nav-number">4.</span> <span class="nav-text">无环加权有向图中的最短路径算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9C%80%E9%95%BF%E8%B7%AF%E5%BE%84"><span class="nav-number">4.1.</span> <span class="nav-text">最长路径</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Bellman-Ford%E7%AE%97%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">Bellman-Ford算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B4%9F%E6%9D%83%E9%87%8D%E7%8E%AF%E7%9A%84%E6%A3%80%E6%B5%8B"><span class="nav-number">5.1.</span> <span class="nav-text">负权重环的检测</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81-1"><span class="nav-number">5.2.</span> <span class="nav-text">实现代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="nav-number">7.</span> <span class="nav-text">参考文献</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E9%82%A3%E7%82%B9%E4%BA%8B%E5%84%BF"><span class="nav-number">8.</span> <span class="nav-text">图的那点事儿</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/27/2017-07-27-Graph_WeightedDigraph/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          图的那点事儿(4)-加权有向图
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2017-07-27 12:00:00" itemprop="dateCreated datePublished" datetime="2017-07-27T12:00:00+08:00">2017-07-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/Algorithms/" itemprop="url" rel="index"><span itemprop="name">Algorithms</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/Algorithms/Graph/" itemprop="url" rel="index"><span itemprop="name">Graph</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>本文作者为: <a target="_blank" rel="noopener" href="https://github.com/SylvanasSun">SylvanasSun</a>.转载请务必将下面这段话置于文章开头处(保留超链接).<br>本文转发自<a target="_blank" rel="noopener" href="https://sylvanassun.github.io/">SylvanasSun Blog</a>,原文链接: <a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/27/2017-07-27-Graph_WeightedDigraph">https://sylvanassun.github.io/2017/07/27/2017-07-27-Graph_WeightedDigraph</a></p>
</blockquote>
<h3 id="加权有向图"><a href="#加权有向图" class="headerlink" title="加权有向图"></a>加权有向图</h3><hr>
<p><code>有向图</code>的实现比<code>无向图</code>更加简单,要实现<code>加权有向图</code>只需要在上一章讲到的<code>加权无向图</code>的实现修改一下即可.</p>
<h4 id="DirectedEdge"><a href="#DirectedEdge" class="headerlink" title="DirectedEdge"></a>DirectedEdge</h4><hr>
<p>由于<code>有向图</code>的边都是带有方向的,所以下面这个实现提供了<code>from()</code>与<code>to()</code>函数,用于获取代表<code>v-&gt;w</code>的两个<code>顶点</code>.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DirectedEdge</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> v;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> w;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">double</span> weight;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DirectedEdge</span><span class="params">(<span class="keyword">int</span> v, <span class="keyword">int</span> w, <span class="keyword">double</span> weight)</span> </span>&#123;</span><br><span class="line">        validateVertexes(v, w);</span><br><span class="line">        <span class="keyword">if</span> (Double.isNaN(weight)) <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Weight &quot;</span> + weight + <span class="string">&quot; is  NaN!&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.v = v;</span><br><span class="line">        <span class="keyword">this</span>.w = w;</span><br><span class="line">        <span class="keyword">this</span>.weight = weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">from</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> v;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">to</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> w;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">weight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> v + <span class="string">&quot;-&gt;&quot;</span> + w + <span class="string">&quot; &quot;</span> + String.format(<span class="string">&quot;%5.2f&quot;</span>, weight);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateVertexes</span><span class="params">(<span class="keyword">int</span>... vertexes)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; vertexes.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (vertexes[i] &lt; <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Vertex &quot;</span> + vertexes[i] + <span class="string">&quot; must be positive number!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="EdgeWeightedDigraph"><a href="#EdgeWeightedDigraph" class="headerlink" title="EdgeWeightedDigraph"></a>EdgeWeightedDigraph</h4><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">EdgeWeightedDigraph</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String NEWLINE = System.getProperty(<span class="string">&quot;line.separator&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// number of vertices in this digraph</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> vertex;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// number of edges in this digraph</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> edge;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// adj[v] = adjacency list for vertex v</span></span><br><span class="line">    <span class="keyword">private</span> Bag&lt;DirectedEdge&gt;[] adj;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// indegree[v] = indegree of vertex v</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[] indegree;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">EdgeWeightedDigraph</span><span class="params">(<span class="keyword">int</span> vertex)</span> </span>&#123;</span><br><span class="line">        String message = String.format(<span class="string">&quot;Vertex %d must be positive number!&quot;</span>, vertex);</span><br><span class="line">        validatePositiveNumber(message, vertex);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.vertex = vertex;</span><br><span class="line">        <span class="keyword">this</span>.edge = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">this</span>.indegree = <span class="keyword">new</span> <span class="keyword">int</span>[vertex];</span><br><span class="line">        <span class="keyword">this</span>.adj = (Bag&lt;DirectedEdge&gt;[]) <span class="keyword">new</span> Bag[vertex];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            adj[v] = <span class="keyword">new</span> Bag&lt;&gt;();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">EdgeWeightedDigraph</span><span class="params">(Scanner scanner)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>(scanner.nextInt());</span><br><span class="line">        <span class="keyword">int</span> edge = scanner.nextInt();</span><br><span class="line">        String message = String.format(<span class="string">&quot;Edge %d must be positive number!&quot;</span>, edge);</span><br><span class="line">        validatePositiveNumber(message, edge);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; edge; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> v = scanner.nextInt();</span><br><span class="line">            <span class="keyword">int</span> w = scanner.nextInt();</span><br><span class="line">            validateVertex(v);</span><br><span class="line">            validateVertex(w);</span><br><span class="line">            <span class="keyword">double</span> weight = scanner.nextDouble();</span><br><span class="line">            addEdge(<span class="keyword">new</span> DirectedEdge(v, w, weight));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">vertex</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> vertex;</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">edge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> edge;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addEdge</span><span class="params">(DirectedEdge e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> v = e.from();</span><br><span class="line">        <span class="keyword">int</span> w = e.to();</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        validateVertex(w);</span><br><span class="line">        adj[v].add(e);</span><br><span class="line">        indegree[w]++;</span><br><span class="line">        edge++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;DirectedEdge&gt; <span class="title">adj</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> adj[v];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">outdegree</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> adj[v].size();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">indegree</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> indegree[v];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在有向图中每条边只会出现一次</span></span><br><span class="line">	<span class="comment">// 遍历边集不需要在无向图里那样为了消除重复边而进行复杂的判断</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;DirectedEdge&gt; <span class="title">edges</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Bag&lt;DirectedEdge&gt; list = <span class="keyword">new</span> Bag&lt;DirectedEdge&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (DirectedEdge e : adj(v)) &#123;</span><br><span class="line">                list.add(e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        StringBuilder s = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        s.append(vertex + <span class="string">&quot; &quot;</span> + edge + NEWLINE);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++) &#123;</span><br><span class="line">            s.append(v + <span class="string">&quot;: &quot;</span>);</span><br><span class="line">            <span class="keyword">for</span> (DirectedEdge e : adj[v]) &#123;</span><br><span class="line">                s.append(e + <span class="string">&quot;  &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            s.append(NEWLINE);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> s.toString();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validatePositiveNumber</span><span class="params">(String message, <span class="keyword">int</span>... numbers)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; numbers.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (numbers[i] &lt; <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(message);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>加权有向图</code>的实现与<code>加权无向图</code>区别不大,而且因为<code>有向图</code>中的边只会出现一次,实现代码要比<code>无向图</code>更简单.</p>
<p><a target="_blank" rel="noopener" href="https://github.com/SylvanasSun/algs4-study/tree/master/src/main/java/chapter4_graphs/C4_4_ShortestPaths">本文中的所有完整代码请到我的GitHub中查看</a></p>
<h3 id="最短路径"><a href="#最短路径" class="headerlink" title="最短路径"></a>最短路径</h3><hr>
<p>“找到一个<code>顶点</code>到达另一个<code>顶点</code>之间的<code>最短路径</code>“是<code>图论</code>研究中的经典算法问题.在<code>加权有向图</code>中,每条<code>有向路径</code>都有一个与之对应的<code>路径权重</code>(路径中所有边的<code>权重</code>之和),要找到一条<code>最短路径</code>其实就是找到<code>路径权重</code>最小的那条路径.</p>
<p><img src="http://algs4.cs.princeton.edu/44sp/images/shortest-path.png" alt="加权有向图中的最短路径"></p>
<h4 id="单点最短路径"><a href="#单点最短路径" class="headerlink" title="单点最短路径"></a>单点最短路径</h4><hr>
<p>“从<code>s</code>到目的地<code>v</code>是否存在一条<code>有向路径</code>,如果有,找出最短的那条路径”.类似这样的问题就是<code>单点最短路径</code>问题,它是我们主要研究的问题.</p>
<p><code>单点最短路径</code>的结果是一棵<code>最短路径树</code>,它是<code>图</code>的一幅<code>子图</code>,<strong>包含了从起点到所有可达顶点的<code>最短路径</code>.</strong></p>
<p>从起点到一个顶点可能存在两条长度相等的路径,如果出现这种情况,可以删除其中一条路径的最后一条边,直到从起点到每个顶点都只有一条路径相连.</p>
<h4 id="最短路径的数据结构"><a href="#最短路径的数据结构" class="headerlink" title="最短路径的数据结构"></a>最短路径的数据结构</h4><hr>
<p><img src="http://algs4.cs.princeton.edu/44sp/images/spt.png"></p>
<p>要实现<code>最短路径</code>的算法还需要借助以下数据结构: </p>
<ul>
<li>edgeTo[]: 一个<code>由顶点索引</code>的<code>DirectedEdge</code>对象的父链接数组,其中<code>edgeTo[v]</code>的值为树中连接<code>v</code>和它的父节点的边.</li>
</ul>
<ul>
<li>distTo[]: 一个<code>由顶点索引</code>的<code>double</code>数组,其中<code>distTo[v]</code>代表从<code>起点</code>到<code>v</code>的已知最短路径的长度.</li>
</ul>
<ul>
<li>初始化时,<code>edgeTo[s]</code>的值为<code>null</code>(<code>s</code>为起点),<code>distTo[s]</code>的值为<code>0.0</code>,从<code>s</code>到不可达的顶点距离为<code>Double.POSITIVE_INFINITY</code>.</li>
</ul>
<h4 id="让边松弛"><a href="#让边松弛" class="headerlink" title="让边松弛"></a>让边松弛</h4><hr>
<p><code>最短路径</code>算法都基于<code>松弛(Relaxation)</code>操作,<strong>它在遇到新的边时,通过更新这些信息就可以得到新的最短路径.</strong></p>
<p>假设对边<code>v-&gt;w</code>进行松弛操作,意味着要先检查从<code>s</code>到<code>w</code>的<code>最短路径</code>是否是先从<code>s</code>到<code>v</code>,然后再由<code>v</code>到<code>w</code>(也就是说<code>v-&gt;w</code>是更短的一条路径),如果是,那么就进行更新.由<code>v</code>到达<code>w</code>的<code>最短路径</code>是<code>distTo[v]</code>与<code>e.weight()</code>之和,如果这个值大于<code>distTo[w]</code>,称这条边松弛失败,并将它忽略.</p>
<p>松弛操作就像用一根橡皮筋沿着连续两个<code>顶点</code>的路径紧紧展开,放松一条边就像将这条橡皮筋转移到另一条更短的路径上,从而缓解橡皮筋的压力.</p>
<p><img src="http://algs4.cs.princeton.edu/44sp/images/relaxation-edge.png" alt="松弛操作的两种情况(失败与成功)"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 松弛一条边</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(DirectedEdge e)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> v = e.from(), w = e.to();</span><br><span class="line">	<span class="comment">// 如果s-&gt;v-&gt;w的路径更小则进行更新</span></span><br><span class="line">    <span class="keyword">if</span> (distTo[w] &gt; distTo[v] + e.weight()) &#123;</span><br><span class="line">        distTo[w] = distTo[v] + e.weight();</span><br><span class="line">        edgeTo[w] = e;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 松弛一个顶点的所有邻接边</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(EdgeWeightedDigraph G, <span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (DirectedEdge e : G.adj(v)) &#123;</span><br><span class="line">        <span class="keyword">int</span> w = e.to();</span><br><span class="line">        <span class="keyword">if</span> (distTo[w] &gt; distTo[v] + e.weight()) &#123;</span><br><span class="line">            distTo[w] = distTo[v] + e.weight();</span><br><span class="line">            edgeTo[w] = e;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Dijkstra算法"><a href="#Dijkstra算法" class="headerlink" title="Dijkstra算法"></a>Dijkstra算法</h3><hr>
<p><code>Dijkstra算法</code>类似于<code>Prim算法</code>,它将<code>distTo[s]</code>初始化为<code>0.0</code>,<code>distTo[]</code>中的其他元素初始化为<code>Double.POSITIVE_INFINITY</code>.然后将<code>distTo[]</code>中最小的<code>非树顶点</code>放松并加入树中,一直重复直到所有的顶点都在树中或者所有的<code>非树顶点</code>的<code>distTo[]</code>值均为<code>Double.POSITIVE_INFINITY</code>.</p>
<p><code>Dijkstra算法</code>与<code>Prim算法</code>都是用添加边的方式构造一棵树:</p>
<ul>
<li><code>Prim算法</code>每次添加的是距离<code>树</code>最近的<code>非树顶点</code>.</li>
</ul>
<ul>
<li><code>Dijkstra算法</code>每次添加的都是<strong>离<code>起点</code>最近的<code>非树顶点</code></strong>.</li>
</ul>
<p>从上述的步骤我们就能看出,<code>Dijkstra算法</code>需要一个优先队列(也可以用<code>斐波那契堆</code>)来保存需要被放松的<code>顶点</code>并确认下一个被放松的<code>顶点</code>(也就是取出最小的).</p>
<p>如此简单的<code>Dijkstra算法</code>也有其缺点,那就是它<strong>只适用于解决<code>权重非负</code>的<code>图</code>.</strong></p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Dijkstra_Animation.gif" alt="Dijkstra算法的运行轨迹"></p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/e/e4/DijkstraDemo.gif"></p>
<h4 id="实现代码"><a href="#实现代码" class="headerlink" title="实现代码"></a>实现代码</h4><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DijkstraSP</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// distTo[v] = distance of  shortest s -&gt; v path</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span>[] distTo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// edgeTo[v] = last edge on shortest s - &gt; v path</span></span><br><span class="line">    <span class="keyword">private</span> DirectedEdge[] edgeTo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// priority queue of vertices</span></span><br><span class="line">    <span class="keyword">private</span> IndexMinPQ&lt;Double&gt; pq;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DijkstraSP</span><span class="params">(EdgeWeightedDigraph digraph, <span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">        validateNegativeWeight(digraph);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> vertex = digraph.vertex();</span><br><span class="line">        <span class="keyword">this</span>.distTo = <span class="keyword">new</span> <span class="keyword">double</span>[vertex];</span><br><span class="line">        <span class="keyword">this</span>.edgeTo = <span class="keyword">new</span> DirectedEdge[vertex];</span><br><span class="line"></span><br><span class="line">        validateVertex(s);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            distTo[v] = Double.POSITIVE_INFINITY;</span><br><span class="line">        distTo[s] = <span class="number">0.0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将起点放入索引优先队列,并不断地进行松弛</span></span><br><span class="line">        pq = <span class="keyword">new</span> IndexMinPQ&lt;&gt;(vertex);</span><br><span class="line">        pq.insert(s, distTo[s]);</span><br><span class="line">        <span class="keyword">while</span> (!pq.isEmpty()) &#123;</span><br><span class="line">            <span class="keyword">int</span> v = pq.delMin();</span><br><span class="line">			<span class="comment">// 对权值最小的非树顶点的所有邻接边集进行松弛操作</span></span><br><span class="line">            <span class="keyword">for</span> (DirectedEdge e : digraph.adj(v))</span><br><span class="line">                relax(e);</span><br><span class="line">        &#125;</span><br><span class="line">		</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// relax edge e and update pq if changed</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(DirectedEdge e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> v = e.from(), w = e.to();</span><br><span class="line">		<span class="comment">// s -&gt; v -&gt; w的权重</span></span><br><span class="line">        <span class="keyword">double</span> weight = distTo[v] + e.weight();</span><br><span class="line">        <span class="keyword">if</span> (distTo[w] &gt; weight) &#123;</span><br><span class="line">            distTo[w] = weight;</span><br><span class="line">            edgeTo[w] = e;</span><br><span class="line">            <span class="keyword">if</span> (pq.contains(w))</span><br><span class="line">                pq.decreaseKey(w, weight);</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                pq.insert(w, weight);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateNegativeWeight</span><span class="params">(EdgeWeightedDigraph digraph)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (DirectedEdge e : digraph.edges()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (e.weight() &lt; <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Edge &quot;</span> + e + <span class="string">&quot; has negative weight.&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">distTo</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> distTo[v];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasPathTo</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> distTo[v] &lt; Double.POSITIVE_INFINITY;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;DirectedEdge&gt; <span class="title">pathTo</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">if</span> (!hasPathTo(v)) <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        Stack&lt;DirectedEdge&gt; path = <span class="keyword">new</span> Stack&lt;DirectedEdge&gt;();</span><br><span class="line">        <span class="keyword">for</span> (DirectedEdge e = edgeTo[v]; e != <span class="keyword">null</span>; e = edgeTo[e.from()]) &#123;</span><br><span class="line">            path.push(e);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> path;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateVertex</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> V = distTo.length;</span><br><span class="line">        <span class="keyword">if</span> (v &lt; <span class="number">0</span> || v &gt;= V)</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;vertex &quot;</span> + v + <span class="string">&quot; is not between 0 and &quot;</span> + (V - <span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述的代码也可以用于处理<code>加权无向图</code>,但需要修改传入的对象类型.不管是<code>无向图</code>还是<code>有向图</code>它们对于<code>最短路径</code>问题是等价的.</p>
<h3 id="无环加权有向图中的最短路径算法"><a href="#无环加权有向图中的最短路径算法" class="headerlink" title="无环加权有向图中的最短路径算法"></a>无环加权有向图中的最短路径算法</h3><hr>
<p>如果是处理<code>无环图</code>的情况下,还会有一种比<code>Dijkstra算法</code>更快、更简单的算法.它的特点如下:</p>
<ul>
<li>能够处理<code>负权重</code>的边.</li>
</ul>
<ul>
<li><p>能够在线性时间内解决单点最短路径问题.</p>
</li>
<li><p>在已知是一张<code>无环图</code>的情况下,它是找出<code>最短路径</code>效率最高的方法.</p>
</li>
<li><p>实现比<code>Dijkstra算法</code>更简单.</p>
</li>
</ul>
<p>只需要将所有<code>顶点</code><strong>按照<code>拓扑排序</code>的顺序</strong>来<code>松弛边</code>,就可以得到这个简单高效的算法.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AcyclicSP</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// distTo[v] = distance  of shortest s-&gt;v path</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span>[] distTo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// edgeTo[v] = last edge on shortest s-&gt;v path</span></span><br><span class="line">    <span class="keyword">private</span> DirectedEdge[] edgeTo;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AcyclicSP</span><span class="params">(EdgeWeightedDigraph digraph, <span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> vertex = digraph.vertex();</span><br><span class="line">        distTo = <span class="keyword">new</span> <span class="keyword">double</span>[vertex];</span><br><span class="line">        edgeTo = <span class="keyword">new</span> DirectedEdge[vertex];</span><br><span class="line"></span><br><span class="line">        validateVertex(s);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            distTo[v] = Double.POSITIVE_INFINITY;</span><br><span class="line">        distTo[s] = <span class="number">0.0</span>;</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line">        Topological topological = <span class="keyword">new</span> Topological(digraph);</span><br><span class="line">        <span class="keyword">if</span> (!topological.hasOrder())</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Digraph is not acyclic.&quot;</span>);</span><br><span class="line">		<span class="comment">// 按照拓扑排序的顺序进行放松操作</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v : topological.order()) &#123;</span><br><span class="line">            <span class="keyword">for</span> (DirectedEdge e : digraph.adj(v))</span><br><span class="line">                relax(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(DirectedEdge e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> v = e.from(), w = e.to();</span><br><span class="line">        <span class="keyword">double</span> weight = distTo[v] + e.weight();</span><br><span class="line">        <span class="keyword">if</span> (distTo[w] &gt; weight) &#123;</span><br><span class="line">            distTo[w] = weight;</span><br><span class="line">            edgeTo[w] = e;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="最长路径"><a href="#最长路径" class="headerlink" title="最长路径"></a>最长路径</h4><hr>
<p>要想找出一条<code>最长路径</code>,只需要把<code>distTo[]</code>的初始化变为<code>Double.NEGATIVE_INFINITY</code>,并更改<code>relax()</code>函数中的不等式的方向.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">AcyclicLP</span><span class="params">(EdgeWeightedDigraph G, <span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">      distTo = <span class="keyword">new</span> <span class="keyword">double</span>[G.vertex()];</span><br><span class="line">      edgeTo = <span class="keyword">new</span> DirectedEdge[G.vertex()];</span><br><span class="line"></span><br><span class="line">      validateVertex(s);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 全部初始化为负无穷</span></span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; G.vertex(); v++)</span><br><span class="line">          distTo[v] = Double.NEGATIVE_INFINITY;</span><br><span class="line">      distTo[s] = <span class="number">0.0</span>;</span><br><span class="line"></span><br><span class="line">      Topological topological = <span class="keyword">new</span> Topological(G);</span><br><span class="line">      <span class="keyword">if</span> (!topological.hasOrder())</span><br><span class="line">          <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Digraph is not acyclic.&quot;</span>);</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> v : topological.order()) &#123;</span><br><span class="line">          <span class="keyword">for</span> (DirectedEdge e : G.adj(v))</span><br><span class="line">              relax(e);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(DirectedEdge e)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">int</span> v = e.from(), w = e.to();</span><br><span class="line"><span class="comment">// 改变不等式的方向</span></span><br><span class="line">      <span class="keyword">if</span> (distTo[w] &lt; distTo[v] + e.weight()) &#123;</span><br><span class="line">          distTo[w] = distTo[v] + e.weight();</span><br><span class="line">          edgeTo[w] = e;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h3 id="Bellman-Ford算法"><a href="#Bellman-Ford算法" class="headerlink" title="Bellman-Ford算法"></a>Bellman-Ford算法</h3><hr>
<p>我们已经知道了处理<code>权重</code>非负图的<code>Dijkstra算法</code>与处理<code>无环图</code>的算法,但如果遇见既含有环,<code>权重</code>也是负数的<code>加权有向图</code>该怎么办?</p>
<p><code>Bellman-Ford算法</code>就是用于处理<code>有环</code>且含有<code>负权重</code>的<code>加权有向图</code>的,它的原理是对图进行<code>V-1</code>次松弛操作,得到所有可能的最短路径.</p>
<p>要实现<code>Bellman-Ford算法</code>还需要以下数据结构: </p>
<ul>
<li>队列: 用于保存即将被松弛的顶点.</li>
</ul>
<ul>
<li>布尔值数组: 用来标记该顶点是否已经存在于队列中,以防止重复插入.</li>
</ul>
<p>我们将起点放入队列中,然后进入一个循环,每次循环都会从队列中取出一个顶点并对其进行松弛.为了保证算法在<code>V</code>轮后能够终止,需要能够动态地检测是否存在<code>负权重环</code>,如果找到了这个环则结束运行(也可以用一个变量动态记录轮数).</p>
<h4 id="负权重环的检测"><a href="#负权重环的检测" class="headerlink" title="负权重环的检测"></a>负权重环的检测</h4><hr>
<p>如果存在了一个从起点可达的<code>负权重环</code>,那么队列就永远不可能为空,为了从这个无尽的循环中解脱出来,算法需要能够动态地检测<code>负权重环</code>.</p>
<p><code>Bellman-Ford算法</code>也使用了<code>edgeTo[]</code>来存放<code>最短路径树</code>中的每一条边,我们根据<code>edgeTo[]</code>来复制一幅图并在该图中检测环.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">findNegativeCycle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">int</span> V = edgeTo.length;</span><br><span class="line"><span class="comment">// 根据edgeTo[]来创建一幅加权有向图</span></span><br><span class="line">      EdgeWeightedDigraph spt = <span class="keyword">new</span> EdgeWeightedDigraph(V);</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; V; v++)</span><br><span class="line">          <span class="keyword">if</span> (edgeTo[v] != <span class="keyword">null</span>)</span><br><span class="line">              spt.addEdge(edgeTo[v]);</span><br><span class="line"><span class="comment">// 判断该图有没有环</span></span><br><span class="line">      EdgeWeightedDirectedCycle finder = <span class="keyword">new</span> EdgeWeightedDirectedCycle(spt);</span><br><span class="line">      cycle = finder.cycle();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h4 id="实现代码-1"><a href="#实现代码-1" class="headerlink" title="实现代码"></a>实现代码</h4><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BellmanFordSP</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span>[] distTo;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> DirectedEdge[] edgeTo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 用于标记顶点是否在队列中</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span>[] onQueue;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 存放下次进行松弛操作的顶点的队列</span></span><br><span class="line">    <span class="keyword">private</span> Queue&lt;Integer&gt; queue;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 计算松弛操作的轮数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> cost;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 负权重环</span></span><br><span class="line">    <span class="keyword">private</span> Iterable&lt;DirectedEdge&gt; cycle;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">BellmanFordSP</span><span class="params">(EdgeWeightedDigraph digraph, <span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> vertex = digraph.vertex();</span><br><span class="line">        <span class="keyword">this</span>.distTo = <span class="keyword">new</span> <span class="keyword">double</span>[vertex];</span><br><span class="line">        <span class="keyword">this</span>.edgeTo = <span class="keyword">new</span> DirectedEdge[vertex];</span><br><span class="line">        <span class="keyword">this</span>.onQueue = <span class="keyword">new</span> <span class="keyword">boolean</span>[vertex];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            distTo[v] = Double.POSITIVE_INFINITY;</span><br><span class="line">        distTo[s] = <span class="number">0.0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Bellman-Ford algorithm</span></span><br><span class="line">        queue = <span class="keyword">new</span> ArrayDeque&lt;&gt;();</span><br><span class="line">        queue.add(s); <span class="comment">// 将起点放入队列</span></span><br><span class="line">        onQueue[s] = <span class="keyword">true</span>; <span class="comment">// 标记起点已在队列中</span></span><br><span class="line">		<span class="comment">// 当队列为空时或者发现负权重环时结束循环</span></span><br><span class="line">        <span class="keyword">while</span> (!queue.isEmpty() &amp;&amp; !hasNegativeCycle()) &#123;</span><br><span class="line">            <span class="keyword">int</span> v = queue.poll();</span><br><span class="line">            onQueue[v] = <span class="keyword">false</span>;</span><br><span class="line">            relax(digraph, v);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">relax</span><span class="params">(EdgeWeightedDigraph G, <span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (DirectedEdge e : G.adj(v)) &#123;</span><br><span class="line">            <span class="keyword">int</span> w = e.to();</span><br><span class="line">            <span class="keyword">double</span> weight = distTo[v] + e.weight();</span><br><span class="line">            <span class="keyword">if</span> (distTo[w] &gt; weight) &#123;</span><br><span class="line">                distTo[w] = weight;</span><br><span class="line">                edgeTo[w] = e;</span><br><span class="line">				<span class="comment">// 将不在队列中的顶点w加到队列</span></span><br><span class="line">                <span class="keyword">if</span> (!onQueue[w]) &#123;</span><br><span class="line">                    queue.add(w);</span><br><span class="line">                    onQueue[w] = <span class="keyword">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">			<span class="comment">// 动态检测负权重环,</span></span><br><span class="line">            <span class="keyword">if</span> (cost++ % G.vertex() == <span class="number">0</span>) &#123;</span><br><span class="line">                findNegativeCycle();</span><br><span class="line">                <span class="keyword">if</span> (hasNegativeCycle()) <span class="keyword">return</span>;  <span class="comment">// found a negative cycle</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><hr>
<p>解决<code>最短路径</code>问题一直都是<code>图论</code>的经典问题,本文中介绍的算法适用于不同的环境,在应用中应该根据不同的环境选择不同的算法.</p>
<table>
<thead>
<tr>
<th>算法</th>
<th>局限性</th>
<th>路径长度的比较次数(增长的数量级)</th>
<th>空间复杂度</th>
<th>优势</th>
</tr>
</thead>
<tbody><tr>
<td>Dijkstra</td>
<td>只能处理正权重</td>
<td>ElogV</td>
<td>V</td>
<td>最坏情况下仍有较好的性能</td>
</tr>
<tr>
<td>拓扑排序</td>
<td>只适用于无环图</td>
<td>E+V</td>
<td>V</td>
<td>实现简单,是无环图情况下的最优算法</td>
</tr>
<tr>
<td>Bellman-Ford</td>
<td>不能存在负权重环</td>
<td>E+V,最坏情况为VE</td>
<td>V</td>
<td>适用广泛</td>
</tr>
</tbody></table>
<h3 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h3><hr>
<ul>
<li><a target="_blank" rel="noopener" href="http://algs4.cs.princeton.edu/44sp/">Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra’s algorithm - Wikipedia</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm">Bellman–Ford algorithm - Wikipedia</a></li>
</ul>
<h3 id="图的那点事儿"><a href="#图的那点事儿" class="headerlink" title="图的那点事儿"></a>图的那点事儿</h3><hr>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/18/2017-07-18-Graph_UndirectedGraph/">图的那点事儿(1)-无向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/23/2017-07-23-Graph_DirectedGraphs/">图的那点事儿(2)-有向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/">图的那点事儿(3)-加权无向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/27/2017-07-27-Graph_WeightedDigraph">图的那点事儿(4)-加权有向图</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/2017/" rel="tag"># 2017</a>
              <a href="/yuwanzi.io/tags/Algorithms/" rel="tag"># Algorithms</a>
              <a href="/yuwanzi.io/tags/Graph/" rel="tag"># Graph</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/" rel="prev" title="图的那点事儿(3)-加权无向图">
                  <i class="fa fa-chevron-left"></i> 图的那点事儿(3)-加权无向图
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2017/07/30/2017-07-30-JavaClosure/" rel="next" title="Java中的闭包之争">
                  Java中的闭包之争 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
